<?php

namespace app\admin\controller;
use app\admin\model\Banner as BannerModel;

class Banner extends Admin
{
    private $position = array(['name'=>'index','title'=>'首页'],['name'=>'mining','title'=>'矿机']);
    /**
     * 轮播图列表
     */
    function index(){

        if (!$this->request->isPost()){
            return view('index',['position'=>json_encode($this->position)]);
        }else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $page = $this->request->post('page', 1, 'intval');
            $order  = $this->request->post('order', '', 'serach_in');	//排序字段
            $sort  = $this->request->post('sort', '', 'serach_in');		//排序方式

            $orderby = ($sort && $order) ? $sort.' '.$order : 'id desc';
            $res = BannerModel::order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

            foreach ($res['data'] as $k => $v){
                foreach ($this->position as $va){
                    if($v['position'] == $va['name']) $res['data'][$k]['position'] = $va['title'];
                }
            }

            $data['status'] = 200;
            $data['data'] = $res;
            return json($data);
        }
    }

    /**
     * 添加轮播图
     * @return \think\response\Json\
     */
    function add(){
        $postField = 'position,pic,e_pic,t_pic,status,level';
        $data = $this->request->only(explode(',',$postField),'post',null);

        $data['add_time'] = date('Y-m-d H:i:s',time());
        $data['update_time'] = date('Y-m-d H:i:s',time());

        $res = BannerModel::insertGetId($data);
        if(empty($res)) return json(['status'=>200,'data'=>$res,'msg'=>'添加失败']);
        return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
    }

    /**
     * 获取轮播图详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getEdit(){
        $postField = 'id';
        $data = $this->request->only(explode(',',$postField),'post',null);
        $res = BannerModel::getDetails($data['id']);
        return json(['status'=>200,'data'=>$res]);
    }

    /**
     * 轮播图修改
     * @return \think\response\Json
     */
    function edit(){
        $postField = 'id,position,pic,e_pic,t_pic,status,level';
        $data = $this->request->only(explode(',',$postField),'post',null);
        //修改文章
        $res = BannerModel::edit($data['id'],$data);
        if(empty($res)) return json(['status'=>200,'data'=>'','msg'=>'修改失败']);
        return json(['status'=>200,'data'=>$res,'msg'=>'修改成功']);
    }

    /**
     * 轮播图删除
     * @return \think\response\Json
     */
    function del(){
        $idx =  $this->request->post('id', '', 'serach_in');
        if(!$idx) throw new ValidateException ('参数错误');
        BannerModel::destroy(['id'=>explode(',',$idx)],true);
        return json(['status'=>200,'msg'=>'操作成功']);

    }




    /**
     * 轮播类型
     * @return \think\response\Json
     */
    function position(){
        $data['status'] = 200;
        $data['data'] = $this->position;
        return json($data);
    }
}